﻿Public Class CusBillManager
    Inherits System.Web.UI.Page

    Public ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString").ToString
    Public objData As New DatabaseClass(ConnectionString)
    Public objDataRead As New DatabaseClass(ConnectionString)
    Public objFunction As New FunctionClass
    Public objAlert As New AlertClass
    Public objConfig As New Configs
    Dim Sql As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            loadData()
        End If
    End Sub



#Region "Grid Cart"

    ' To mau cho tung Row
    Private Sub gridBill_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridBill.RowCreated
        'Add CSS class on normal row.
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Normal Then
            e.Row.CssClass = "alt1"
        End If
        'Add CSS class on alternate row.
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Alternate Then
            e.Row.CssClass = "alt"
        End If
    End Sub

#End Region

#Region "My Function"

    ' Kiem tra du lieu va load data cua khach hang
    Public Sub loadData()
        Try
            ' Kiem tra don hang co san pham hay k, neu khong co redirect ve trang chu
            If (Session("CusID") <> Nothing) Or (Session("CusID") <= 0) Then
                Dim idCus As Integer = 0
                idCus = Integer.Parse(Session("CusID").ToString)
                If idCus = 0 Then
                    Response.Redirect("\")
                    Exit Sub
                End If
                Sql = "spo_BillGetByCusID " & objFunction.ValueCheck(idCus)
                Dim ds As DataSet = objData.ExeDataset(Sql)
                gridBill.DataSource = ds.Tables(0)
                gridBill.DataBind()
            End If
        Catch ex As Exception
            Response.Redirect("\")
        End Try
    End Sub

    ' Lấy dự liệu Sản Phẩm đưa vào Grid View Child
    Public Sub BindGridChild(ByVal idBill As Integer, ByVal objGridSubCat As GridView, ByVal sortEx As String)
        Try
            Sql = "spi_BillGetDetail " & objFunction.ValueCheck(idBill)
            
            'MsgBox(sql)
            'lbSql.Text = sql
            Dim dts As DataSet = objDataRead.ExeDataset(Sql)
            Dim dtt As DataTable = dts.Tables(2)

            objGridSubCat.DataSource = dtt
            objGridSubCat.DataBind()

        Catch ex As Exception
            
        End Try
    End Sub


    ' Ham hien thi ngay thang tren grid view
    Public Function formatTime(ByVal str As String) As String
        Dim tempDate As New Date
        tempDate = CDate(str)
        Dim strDateReturn = tempDate.Day & "-" & tempDate.Month & "-" & tempDate.Year
        Dim strTimeReturn = tempDate.Hour & ":" & tempDate.Minute & "  " & tempDate.Second
        Return strDateReturn & "<span style=""font-style:italic;font-size:11px;color: #808080"">(" & strTimeReturn & ")</span>"
    End Function

    ' Format status trên grid view
    Public Function formatStatus(ByVal srtStt As String) As String
        Dim stt As Integer = 0
        Try
            stt = Integer.Parse(srtStt)
            If stt = 1 Then
                Return "<span style=""font-weight:bold;color: orange;""><b>Đã duyệt</b></span>"
            ElseIf stt = 2 Then
                Return "<span style=""font-weight:bold;color: green;""><b>Đã thanh toán</b></span>"
            Else
                Return "<span style=""font-weight:bold;color: red;""><b>Chờ duyệt</b></span>"
            End If
        Catch ex As Exception

        End Try

    End Function

    ' Format định dạng tiền VND trên Grid
    Public Function formatCurrency(ByVal currency As String) As String
        Dim temp As Double = Double.Parse(currency)
        Return String.Format("{0:#,##0}", temp) & " đ"
    End Function

#End Region



    Private Sub gridBill_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridBill.RowDataBound
        Try
            ' Khai bao doi tuong label de lay ID category
            Dim lbIDBill As Label = CType(e.Row.FindControl("lbl_ID"), Label)
            ' Khai bao bien ID category luu gia tri cua label ID
            Dim idBill As Integer = Integer.Parse(lbIDBill.Text)
            ' Khai bao doi tuong grid Sub Cat de fill du lieu
            Dim objGrid As GridView = CType(e.Row.FindControl("gridChild"), GridView)
            ' khai bao input image toggle de add script onclick
            Dim imgToggle As ImageButton = CType(e.Row.FindControl("imgToggle"), ImageButton)
            Dim idDiv As String = (e.Row.RowIndex + 2).ToString("00")

            imgToggle.Attributes.Add("onclick", "return showDetail('" & idDiv & "')")
            BindGridChild(idBill, objGrid, "")
        Catch ex As Exception

        End Try
    End Sub
End Class